package netmorph.model;

import model.AbstractModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;


import java.util.ArrayList;

import java.util.List;
import neuron.Network;
import neuron.NetworkStatistics;
import neuron.file.NetmorphLoader;

/**
 * @deprecated FIXME remove this class, it has not much function anymore.. see NetmorphExec and ConfigFile
 * @author Risto Laakso <risto.laakso@gmail.com>
 */
public class NetmorphModel extends AbstractModel<File> {

	 private NetmorphExec exec = null;
    public ConfigFile config = null;
    private NetworkStatistics lastStats;


	 /**
	  * Parse results from netmorph run. Takes .neurons file name as paramater to determine name and location of result files.
	  * @param neurons - name and path of .neurons file
	  * @return
	  * @throws FileNotFoundException
	  * @throws IOException
	  * @throws ParseException
	  */
    public static NetworkStatistics parseResults(File neurons) throws FileNotFoundException, IOException, ParseException {
        return NetmorphLoader.load(neurons).statistics();
    }

	@Override
	public void initRun(String id, long randSeed) throws IOException {
	    process = null; //net = null; stats = null;
		 exec = new NetmorphExec(config, id);
		 setParam("randseed", ""+randSeed);
		 exec.configure();
		 process = exec;
	 }

	@Override
	public void parseResults() {

		List<Network> nets = new ArrayList<Network>();

		for (File f : results) {
			try {
				nets.add(NetmorphLoader.load(f));
			} catch (Exception e) { e.printStackTrace(); }
		}

		lastStats = new NetworkStatistics(nets);
	}


	/**
	 * Get a list of warnings from simulation
	 * @return
	 */
	public List<String> getWarnings()
	{
		if (exec == null) return null;
		return exec.getWarnings();
	}

	/**
	 * Initialize simulation and create configuration file contents based on current parameters
	 * @return
	 */
    public String createConfig() {
        exec = new NetmorphExec(config);
        return exec.createConfigAsString();
    }

    public NetmorphModel() {
        config = new ConfigFile();
    }

	 public void loadConfig(File in) throws FileNotFoundException, IOException
	 {
		  config = new ConfigFile(in);
	 }

	 public void saveConfig(File out) throws FileNotFoundException, IOException
	 {
		  config.writeFile(out);
	 }

	 /**
	  * Get model parameter as string
	  * @param key
	  * @return
	  */
    public String getParam(String key) {
        return config.get(key);
    }

	 /**
	  * Get model parameter as double
	  * @param key
	  * @return
	  */
	 public double getParamD(String key) {
        return Double.parseDouble(config.get(key));
    }

	 /**
	  * Set model parameter as double
	  * @param key
	  * @param value
	  */
	 public void setParamD(String key, double value) {
		 config.put(key, ""+value);
	 }

	 /**
	  * Set model parameter as string
	  * @param key
	  * @param value
	  */
    public void setParam(String key, String value) {
        config.put(key, value);
    }

    public NetworkStatistics getLastStats() {
        return lastStats;
    }

}
